<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客正文</title>
    <link rel="stylesheet" href="css/common.css?v=1">
    <link rel="stylesheet" href="css/blog_content.css">
    <link rel="stylesheet" href="editor.md/css/editormd.preview.min.css"/>
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="js/util.js"></script>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="img/logo.jpg" alt="">
    <span class="title">我的博客系统</span>
    <!-- 用来占据中间位置 -->
    <span class="spacer"></span>
    <a href="blog_list.html">主页</a>
    <a href="blog_edit.html">写博客</a>
    <a href="myblog_list.html">个人中心</a>
</div>
<!-- 版心 -->
<div class="container">
    <!-- 左侧个人信息 -->
    <div class="container-left">
        <div class="card">
            <img id="photo" src="img/avatar.webp" class="avtar" alt="">
            <h3 id="username"></h3>
             <a href="https://gitee.com/">gitee 地址</a>
            <div class="counter">
                发表总文章数：<span id="artCount"></span>
            </div>
        </div>
    </div>
    <!-- 右侧内容详情 -->
    <div class="container-right">
        <div class="blog-content">
            <!-- 博客标题 -->
            <h3 id="title"></h3>
            <!-- 博客时间 -->
            <div class="date">
                <span id="createtime"></span> | 访问量：<span id="rcount"></span>
            </div>
            <!-- 博客正文 -->
            <div id="editorDiv">

            </div>
            <hr>
            <div>
                <h2 style="text-align: left;margin: 20px 0 20px 0;">评论</h2>
                <div id="addcomment">
                    <div>
                        <textarea id="comment_content" placeholder="此处输入评论内容" style="text-align: left;width: 50%;height: 100px;"></textarea>
                    </div>
                    <div>
                        <span id="comment_login_name" style="margin-left: 90px">请先登录</span>
                        <input type="button" onclick="addComment()" value="发表评论" class="btn" style="margin-left: 40px;margin-top: 10px;">
                    </div>
                </div>
                <h3 id="commentCount" style="text-align: left;margin-top: 10px;"></h3>
                <div id="commentlist">

                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var aid = getUrlParam("aid");//得到文章id
    var editormd;
    var isLogin = false;//是否登录

    function initEdit(md) {
        editormd = editormd.markdownToHTML("editorDiv", {
            markdown: md, // Also, you can dynamic set Markdown text
            // htmlDecode : true,  // Enable / disable HTML tag encode.
            // htmlDecode : "style,script,iframe",  // Note: If enabled, you should filter some dangerous HTML tags for website security.
        });
    }

    // 获取文章的详细信息 [不拦截]
    function getArtDetail() {
        if (aid != null && aid > 0) {
            // 访问后端查寻文章详情
            jQuery.ajax({
                url: '/art/detail',
                type: 'GET',
                data: {"aid": aid},
                success: function (body) {
                    if (body.code == 200 && body.data != null) {
                        // 填充标题
                        let title = document.querySelector('#title');
                        title.innerHTML = body.data.title;
                        // 填充日期
                        let createtime = document.querySelector('#createtime');
                        createtime.innerHTML = formatDate(body.data.createtime);
                        // 填充访问量
                        let rcount = document.querySelector('#rcount');
                        rcount.innerHTML = body.data.rcount+1;  //为了与数据库保持相同
                        // 填充内容
                        initEdit(body.data.content);
                        // 左侧个人信息
                        if (body.data.photo!=null&&body.data.photo!=""){
                            let photo=document.querySelector("#photo");
                            photo.src=body.data.photo;
                        }
                        let username=document.querySelector("#username");
                        username.innerHTML=body.data.username;
                        let artCount=document.querySelector("#artCount");
                        artCount.innerHTML=body.data.artCount;
                    }
                }
            });
        }
    }

    // 更新访问量
    function updateCount() {
        jQuery.ajax({
            url: '/art/rcount',
            type: 'POST',
            data: {
                "aid": aid
            },
            success: function (res) {
            }
        })
    }

    // 获取评论列表
    function getCommentList() {
        jQuery.ajax({
            url:"/comment/list",
            type:"GET",
            data:{
                "aid":aid
            },
            success: function (body) {
                if (body.code == 200 && body.data != null) {
                    var commentListHtml = "";
                    for (let i = 0; i < body.data.length; i++) {
                        var comment = body.data[i];
                        commentListHtml += '<div style="margin-bottom: 26px;">';
                        commentListHtml += comment.username + '：' + comment.content;
                        commentListHtml += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class="comment_del_class" style="display: none;color: #e92525;text-decoration: none" href="javascript:del(' + comment.cid + ')">删除</a>';
                        commentListHtml += "</div>";
                    }
                    jQuery('#commentlist').html(commentListHtml);
                    var commentCount = body.data.length;
                    jQuery("#commentCount").html('共 ' + commentCount + ' 条评论');
                }
            }
        });
    }

    // 获取当前用户登录信息
    function getLoginUser() {
        jQuery.ajax({
            url: '/user/info',
            type: 'GET',
            data: {},
            success: function (body) {
                if (body.code == 200 && body.data != null && body.data.uid >= 0) {
                    // 当前用户登录了
                    isLogin = true;
                    jQuery('#comment_login_name').html(body.data.username);
                    // 判断当前文章是否是当前用户发表的
                    isArticleByMe(aid);
                } else {
                    // 当前用户未登录
                }
            }
        })
    }

    // 判断当前文章是否是当前用户发表的
    function isArticleByMe(aid) {
        jQuery.ajax({
            url: '/user/isartbyme',
            type: 'GET',
            data: {
                "aid": aid
            },
            success: function (body) {
                if (body.code == 200 && body.data == 1) {
                    // 当前文章归属于当前登录用户
                    jQuery('.comment_del_class').each(function (i) {
                        jQuery(this).show();
                    })
                }
            }
        })
    }

    // 发表评论
    function addComment() {
        // 评论正文
        var comment_content = jQuery('#comment_content');
        // 非空判断
        if (comment_content.val().trim() == "") {
            alert('请先输入评论的内容！');
            comment_content.focus();
            return false;
        }
        // 判断是否登录
        if (!isLogin) {
            alert('请先登录！');
            return false;
        }
        // 将前端数据发送给后端（文章id、评论内容）
        jQuery.ajax({
            url: '/comment/add',
            type: 'POST',
            data: {
                "aid": aid,
                "content": comment_content.val()
            },
            success: function (body) {
                // 将后端返回的数据显示给用户
                if (body.code == 200 && body.data == 1) {
                    // 评论添加成功
                    alert('评论发表成功！');
                    // 刷新评论
                    location.href = location.href;
                } else {
                    alert('抱歉，发表失败，请重试！' + body.msg);
                }
            }
        })


    }

    // 评论删除功能
    function del(cid) {
        if (!confirm('确定删除？')){
            return false;
        }
        // 1.参数校验
        if (cid == "" || cid <= 0) {
            alert('抱歉，操作失败，请刷新页面之后重试！');
            return false;
        }
        if (aid == "" || aid <= 0) {
            alert('抱歉，评论删除失败，请刷新页面之后重试！');
            return false;
        }
        // 2.发送数据给后端（评论的id，文章的id）
        jQuery.ajax({
            url: '/comment/del',
            type: 'POST',
            data: {
                "aid": aid,
                "cid": cid
            },
            success: function (body) {
                // 3.将后端结果显示给用户
                if (body.code == 200 && body.data == 1) {
                    // 删除成功
                    alert('恭喜，评论删除成功！')
                    location.href = location.href;
                } else {
                    alert('抱歉，评论删除失败！' + body.msg);
                }
            }
        })


    }

    updateCount();
    getArtDetail();
    getCommentList();
    getLoginUser();

</script>
</body>

</html>